package com.cz.nongyetongv5.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.JianDuJianChaPageAdapter;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.JianDuJianChaRecord;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.TextProgressBar;
import com.cz.nongyetongv5.custom.badgeview.BadgeView;
import com.cz.nongyetongv5.custom.dialog.CustomDialog;
import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.utils.GoHomeUtil;
import com.cz.nongyetongv5.utils.SerliazableUtil;

public class JianDuJianChaMainActivity extends FragmentActivity implements
		OnPageChangeListener, OnClickListener {

	/**
	 * 返回的按钮
	 */
	private ViewGroup btnBack = null;

	/**
	 * 滑动的页面
	 */
	private ViewPager vpJianDuJianChaPager = null;

	/**
	 * 确定的按钮
	 */
	private View btnSureJianDuItem = null;

	/**
	 * 数据库帮助初始化
	 */
	private DatabaseHelper db = null;

	private User user = null;

	/**
	 * 初始化组件
	 */
	private void initView() {
		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		vpJianDuJianChaPager = (ViewPager) findViewById(R.id.vpJianDuJianChaPager);

		List<Fragment> activities = new ArrayList<Fragment>();
		activities.add(new JianDuJianChaNewRecordActivity());

		activities.add(new JianDuJianChaUploadActivity());

		vpJianDuJianChaPager.setAdapter(new JianDuJianChaPageAdapter(
				getSupportFragmentManager(), activities));

		vpJianDuJianChaPager.setOnPageChangeListener(this);

		btnSureJianDuItem = findViewById(R.id.btnSureJianDuItem);

		btnSureJianDuItem.setOnClickListener(this);

	}

	/**
	 * 进行组件的配置,如 设置默认选中等操作
	 */
	private void initViewConfig() {

		user = (User) getIntent().getSerializableExtra("user");

		// 初始化数据库工具
		db = new DatabaseHelper(this, CommonConstants.DBNAME, null,
				CommonConstants.DATABASE_VERSION);

		// 设置第一页为选中
		vpJianDuJianChaPager.setCurrentItem(0);
		onPageSelected(0);

	}

	/**
	 * 显示待上传的个数的红点
	 *
	 * @param view
	 */
	private void showHongDian(View view) {

		// 查询存在记录的个数
		int count = SerliazableUtil.existsCount(user.getUsername());

		int selectPosition = vpJianDuJianChaPager.getCurrentItem();

		BadgeView badgeView = (BadgeView) view.getTag();

		if (badgeView == null) {

			int topVal = Utils.dip2px(this, 3);

			badgeView = new BadgeView(this, view);
			badgeView.setBadgeMargin(
					badgeView.getLeft() + badgeView.getWidth(),
					badgeView.getTop() + topVal);

			view.setTag(badgeView);
		}

		badgeView.setText(count + "");

		badgeView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);

		if (selectPosition == 0 && count > 0) {
			badgeView.show();
		} else {
			badgeView.hide();
		}

	}

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.jiandujiancha_main);

		// 进行组件的初始化
		initView();

		// 进行组件的配置
		initViewConfig();

	}

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		super.onDestroy();
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int position) {

		ViewGroup btnNewRecord = (ViewGroup) findViewById(R.id.btnNewRecord);

		btnNewRecord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				vpJianDuJianChaPager.setCurrentItem(0);
			}
		});

		ViewGroup btnExistsRecord = (ViewGroup) findViewById(R.id.btnExistsRecord);

		btnExistsRecord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				vpJianDuJianChaPager.setCurrentItem(1);
			}
		});

		ImageView ivDaiShangChuanCount = (ImageView) findViewById(R.id.ivDaiShangChuanCount);

		// 显示待上传的红点
		showHongDian(ivDaiShangChuanCount);

		if (position == 0) {

			btnNewRecord.findViewById(R.id.imageview).setSelected(true);

			btnExistsRecord.findViewById(R.id.ivDaiShangChuanCount)
					.setSelected(false);

			if (btnSureJianDuItem != null
					&& btnSureJianDuItem.getVisibility() != View.VISIBLE) {
				btnSureJianDuItem.setVisibility(View.VISIBLE);
			}

		}

		if (position == 1) {
			btnNewRecord.findViewById(R.id.imageview).setSelected(false);
			btnExistsRecord.findViewById(R.id.ivDaiShangChuanCount)
					.setSelected(true);

			if (btnSureJianDuItem != null
					&& btnSureJianDuItem.getVisibility() != View.INVISIBLE) {

				btnSureJianDuItem.setVisibility(View.INVISIBLE);

			}

		}
	}

	@Override
	public void onBackPressed() {

		if (vpJianDuJianChaPager.getCurrentItem() == 1) {
			finish();
		} else {

			Fragment fragment = getSupportFragmentManager().getFragments().get(
					0);

			if (fragment instanceof JianDuJianChaNewRecordActivity) {
				final JianDuJianChaNewRecordActivity activity = (JianDuJianChaNewRecordActivity) fragment;

				if (!activity.isEditor()) {

					final CustomDialog customDialog = new CustomDialog(this);

					customDialog.setHeightPercent(0.35f);

					customDialog
							.setOnCancleClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {

									customDialog.dismiss();

									SerliazableUtil.deleteTemp();
									finish();

								}
							});

					customDialog
							.setOnActionClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									customDialog.dismiss();

									finish();

								}
							});

					customDialog
							.setNote("存储，暂存当前录入的信息，下次进入编辑时数据还在；\n放弃，下次进入编辑时数据是空白的。");

					customDialog.setMessage("您是否需要暂存此记录?");
					customDialog.setCancleTxt("放弃");
					customDialog.setActionTxt("存储");

					customDialog.show();
				} else {
					finish();
				}

			}

		}

	};

	@Override
	public void onClick(View view) {

		if (view == btnBack) {

			onBackPressed();
		}

		if (view == btnSureJianDuItem) {

			if (getSupportFragmentManager() != null
					&& getSupportFragmentManager().getFragments() != null) {

				Fragment fragment = getSupportFragmentManager().getFragments()
						.get(0);

				Fragment daiShangChuanFragment = getSupportFragmentManager()
						.getFragments().get(1);

				if (fragment != null) {
					JianDuJianChaNewRecordActivity newRecordActivity = (JianDuJianChaNewRecordActivity) fragment;

					boolean result = newRecordActivity.submit();

					if (result) {
						ImageView ivDaiShangChuanCount = (ImageView) findViewById(R.id.ivDaiShangChuanCount);

						// 显示待上传的红点
						showHongDian(ivDaiShangChuanCount);

						// 让另一个页面重新加载数据
						if (daiShangChuanFragment != null) {

							((JianDuJianChaUploadActivity) daiShangChuanFragment)
									.reloadData();

						}

					}

				}

			}

		}

	}

	/**
	 * 进入编辑
	 */
	public void toEdit(JianDuJianChaRecord record) {

		if (record != null) {

			vpJianDuJianChaPager.setCurrentItem(0);

			Fragment fragment = getSupportFragmentManager().getFragments().get(
					0);

			if (fragment != null) {

				JianDuJianChaNewRecordActivity newRecordActivity = (JianDuJianChaNewRecordActivity) fragment;

				newRecordActivity.loadEditMode(record);

			}

		}

	}
}
